<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

// 自定义模块，自定义区块，自定义配置
// 自定义栏目，自定义插件，自定义扩展，自定义单页，自定义分类

//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//

// 热门标签
function seohotkeywords($content='',$tablename='',$classid=0,$bigclassname='',$titlename='',$keywordname='',$classidname='')
{
	/* 关键词替换为描文本 */
	$keyowrds_list = configmodule($tablename,$classid,0,$bigclassname,"",'default',$classidname,100);
	foreach($keyowrds_list as $key=>$val)
	{
		$updates[$val[$titlename]] = $val[$keywordname];
	}

	$keywords = array_keys($updates);
	$urls = array_values($updates);
	foreach ($keywords as $k => $v) {
		if (preg_match_all("/<a .*?>{$v}<\/a>/i", $content)) {
			$content = preg_replace("/<a .*?>{$v}<\/a>/i", "{$v}", $content);
		}
		if (preg_match_all("/{$v}/i", $content)) {
			$content = str_replace($v, "<a href=" . $urls[$k] . " target='_blank' title='{$v}'>{$v}</a>", $content);
		}
	}
	
	return $content;
}

// 网站基本页面title,keyword,description
function seoheadmeta($title='',$keyword='',$description='',$hometitle='',$content="")
{
	$title = $title . "-" . $hometitle;
	
	// 描述为空时自动提取内容200个字符
	if(empty($description) && !empty($content))seodetaildescription($content);
		
	return array('title'=>$title,'keyword'=>$keyword,'description'=>$description);
}

// 网站当前位置
// htmlinfo($homehtml, $herehtml, $herenowhtml, $titlehtml)
function seourhere($tablename="", $classid="", $classidname="", $bigclassname="", $classnamename="", $htmlinfo="")
{
	global $configlang,$configinfo;
	
	$cate1 = $cate2 = $cate3 = "";
	
	$homehtml = $htmlinfo['homehtml'];
	$herehtml = $htmlinfo['herehtml'];
	$herenowhtml = $htmlinfo['herenowhtml'];
	$titlehtml = $htmlinfo['titlehtml'];
	
	// 首页
	$homehtml = str_replace("{lang}",$configinfo['lang'],$homehtml);
	$urhere = str_replace("{name}",$configlang['home'],$homehtml);
	
	// 分类层级
	if($tablename)
	{
		$current3info = columninfo($tablename,$classid,$classidname);
		if($configinfo['columnattrok'] == 1 && $configinfo['langok'] == 1 && $configinfo['appmodule'] == 1)
		{
			$attrother = modulelangcolumn($current3info,$current3info[$classidname]);
			$current3info[$classnamename] = $attrother[$classnamename];
		}
		if($current3info['index_num'] != '自定义区块' && $current3info['index_num'] != '自定义配置' && $current3info['index_num'] != '自定义扩展')
		{
			if($titlehtml)
			{
				$cate3 = str_replace("{id}",$current3info[$classidname],$herehtml);
				$cate3 = str_replace("{name}",$current3info[$classnamename],$cate3);
				$cate3 = str_replace("{lang}",$configinfo['lang'],$cate3);
			}else{
				$cate3 = str_replace("{name}",$current3info[$classnamename],$herenowhtml);
			}
			if($current3info[$bigclassname])
			{
				$current2info = columninfo($tablename,$current3info[$bigclassname],$classidname);
				if($configinfo['columnattrok'] == 1 && $configinfo['langok'] == 1 && $configinfo['appmodule'] == 1)
				{
					$attrother = modulelangcolumn($current2info,$current2info[$classidname]);
					$current2info[$classnamename] = $attrother[$classnamename];
				}
				if($current2info['index_num'] != '自定义区块' && $current2info['index_num'] != '自定义配置' && $current2info['index_num'] != '自定义扩展')
				{
					$cate2 = str_replace("{id}",$current2info[$classidname],$herehtml);
					$cate2 = str_replace("{name}",$current2info[$classnamename],$cate2);
					$cate2 = str_replace("{lang}",$configinfo['lang'],$cate2);
					if($current2info[$bigclassname])
					{
						$current1info = columninfo($tablename,$current2info[$bigclassname],$classidname);
						if($configinfo['columnattrok'] == 1 && $configinfo['langok'] == 1 && $configinfo['appmodule'] == 1)
						{
							$attrother = modulelangcolumn($current1info,$current1info[$classidname]);
							$current1info[$classnamename] = $attrother[$classnamename];
						}
						if($current1info['index_num'] != '自定义区块' && $current1info['index_num'] != '自定义配置' && $current1info['index_num'] != '自定义扩展')
						{
							$cate1 = str_replace("{id}",$current1info[$classidname],$herehtml);
							$cate1 = str_replace("{name}",$current1info[$classnamename],$cate1);
							$cate1 = str_replace("{lang}",$configinfo['lang'],$cate1);
						}
					}
				}
			}
		}
		$urhere .= $cate1 .$cate2 .$cate3;
	}
	
	// 标题
	if($titlehtml)
	{
		$urhere .= $titlehtml;
	}
	
	return $urhere;
}

/**
 * 提取描述文字
 * @param  string  $content    描述文字
 * @return array               提取后的描述文字
 */
function seodetaildescription($content='',$sub=200){
	$content=str_replace('&nbsp;','',$content);
	$content=str_replace(' ','',$content);
	$desc = strip_tags($content);
	$desc = str_replace("\n", '', $desc);
	$desc = str_replace("\r", '', $desc);
	$desc = str_replace("\t", '', $desc);
	$desc = mb_substr($desc,0,$sub,'utf-8');
	return $desc;
}

// 当前城市
function seocitytab($tablename='',$classid=0,$bigclassname='',$domainname='',$citytitlename='',$dataidname='',$seoinfoname=array(),$thseoinfo=array())
{
	global $cjdb;
	
	$titlename = $seoinfoname['titlename'];
	$keywordname = $seoinfoname['keywordname'];
	$descriptionname = $seoinfoname['descriptionname'];
	$title = $thseoinfo['title'];
	$keyword = $thseoinfo['keyword'];
	$description = $thseoinfo['description'];
	$homesite = $thseoinfo['homesite'];
	
	/* 分站 */
	$site = explode(".", $_SERVER['HTTP_HOST']);
	$sitename = $site[0];
	
	$sql = "SELECT * FROM $tablename WHERE $bigclassname in($classid) and $domainname = '$sitename'";
	$siteArr = $cjdb->getRow($sql);
	
	if(is_array($siteArr))
	{
		$cityid = $siteArr[$dataidname];
		$cityname = $siteArr[$titlename];
	
		if(!empty($siteArr[$descriptionname]))
		{
			$siteSeo['title'] = $siteArr[$citytitlename];	
			$siteSeo['keyword'] = $siteArr[$keywordname];
			$siteSeo['description'] = $siteArr[$descriptionname];
		}else{
			$siteSeo['title'] = str_replace($homesite,$cityname,$title);
			$siteSeo['keyword'] = str_replace($homesite,$cityname,$keyword);
			$siteSeo['description'] = str_replace($homesite,$cityname,$description);
		}
	}else{
		$cityid = 0;
		$cityname = $homesite;
		$siteSeo['title'] = str_replace($homesite,$cityname,$title);
		$siteSeo['keyword'] = str_replace($homesite,$cityname,$keyword);
		$siteSeo['description'] = str_replace($homesite,$cityname,$description);
	}
	
	return array('cityid' => $cityid, 'cityname' => $cityname, 'siteseo' => $siteSeo);
}

// 切换城市
function seocitytablist($citylist=array(),$tablename='',$classidname='',$bigclassname='',$where='',$order='',$dataidname='')
{
	foreach($citylist as $key=>$val)
	{
		$citylists['all'][$key] =$val ;
		$citylists['all'][$key]['twolist'] = configmodule($tablename,$val[$classidname],0,$bigclassname,$where,$order,$dataidname);
		$citylists[$val['index_num']][$key] =$val ;
		$citylists[$val['index_num']][$key]['twolist'] = configmodule($tablename,$val[$classidname],0,$bigclassname,$where,$order,$dataidname);
	}
	
	return $citylists;
}

// 右侧最新标签
function seorighttablist($taglist_news=array())
{
	$randcolor = array(0=>"red",1=>'#db5f00',3=>'#fc993e',4=>'#edd104',5=>'#77bc29',6=>'#50a0db',7=>'#aaa',8=>'#000');
	$randfont = array(0=>"12",1=>'14',3=>'16',4=>'18',5=>'20',6=>'22',7=>'24',8=>'28');
	foreach($taglist_news as $key=>$val)
	{
		$taglist_news[$key]['color'] = $randcolor[rand(0,8)];
		$taglist_news[$key]['font'] = $randfont[rand(0,8)];	
	}
	
	return $taglist_news;
}

// 热门标签
function seohottaglist($tablename='',$classid=0,$bigclassname='',$dataidname='',$tagclass='',$order="",$limit="60")
{
	global $cjdb;
	
	if($order)$order = ",$order";
	if($limit)$limit = "limit 0,$limit";
	$sql = "SELECT tl.*,count(lt.$dataidname) AS total FROM $tablename AS tl " . 
	"LEFT JOIN $tablename AS lt ON lt.$dataidname = pt.listid AND lt.$bigclassname IN (".$tagclass.")  " . 
	"WHERE tl.$bigclassname IN (".$classid.") group by tl.$dataidname order by total desc $order $limit";
	$hottaglist = $cjdb->getAll($sql);
	
	return $hottaglist;	
}

// 标签库
function seoinserttag($taglist=array(),$tablename='',$datanamename='',$bigclassname='',$keywordname='',$topid=0,$attrtagname='')
{
	global $cjdb,$configinfo;
	
	if($taglist)
	{
		$tag = explode(",", $taglist);
		
		if($configinfo['config']==2)
		{
			$sql = "SELECT * FROM $tablename WHERE $keywordname = '$topid'";
			$datainfo = $cjdb->getRow($sql);
		}
		// 新增模块程序配置
		if($configinfo['config']==1)
		{
			$attrtagname = 'attrtag';
			$datainfo[$attrtagname] = $configinfo['module'][$topid]['attrtag'];	
		}
		
		if($datainfo[$attrtagname])
		{
			foreach($tag as $key=>$val)
			{
				if(!empty($val))
				{
					$sql = "SELECT * FROM $tablename WHERE $datanamename = '$val' and $bigclassname = '$datainfo[$attrtagname]'";
					$taginfo = $cjdb->getRow($sql);
					
					if(empty($taginfo))
					{
						$sql = "INSERT INTO $tablename ($datanamename,$bigclassname) ". "VALUES ('".$val."','".$datainfo[$attrtagname]."')";
						$cjdb->query($sql);	
					}
				}
			}
		}
	}
}

/* 通过TAG名称获取当前TAG信息 */
function seotaginfo($tablename='',$classid=0,$bigclassname='',$tagname='',$tagnamename='')
{
	global $cjdb;
	
	$sql = "SELECT * FROM $tablename WHERE $bigclassname = '$classid' and $tagnamename = '$tagname'";
	$taginfo = $cjdb->getRow($sql);

	return $taginfo;	
}

// 标签SEO
function seoheadtag($title='',$keyword='',$description='',$hometag='',$tagname='')
{
	$tagSeo['title'] = str_replace($hometag,$tagname,$title);
	$tagSeo['keyword'] = str_replace($hometag,$tagname,$keyword);
	$tagSeo['description'] = str_replace($hometag,$tagname,$description);	
	
	return $tagSeo;
}
?>